﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.AutoUpdate;
using DWF.Studio.Custom;
using DWF.Studio.ISerivces;
using DWF.Studio.Models;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace DWF.Studio.Services
{
    internal class AppService : IAppService
    {
        private static readonly AppService _singleAppService = new AppService();

        private bool _isRestart = false;

        private int _currentLanguageId = 0;

        /// <summary>
        /// 工程管理类引用
        /// </summary>
        public static AppService Instance
        {
            get
            {
                return _singleAppService;
            }
        }

        private AppService()
        {

        }

        public bool CheckSingleInstance()
        {
            bool isNewInstance = true;

            return isNewInstance;
        }

        public void Close()
        {
            App.Current.MainWindow.Close();
        }

        public void Restart()
        {
            _isRestart = true;
            App.Current.MainWindow.Close();
        }

        public void FinalHandle()
        {
            if (_isRestart)
            {
                System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
            }
        }

        public void Shutdown()
        {
            App.Current.Shutdown();
        }

        public IEnumerable<Language> GetLanguages()
        {
            var languages = new List<Language>();
            languages.Add(new Language() { Name = "简体中文", Value = "zh-CN", Id = 0 });
            languages.Add(new Language() { Name = "中文繁體", Value = "zh-TW", Id = 3 });
            languages.Add(new Language() { Name = "English", Value = "en", Id = 1 });
            languages.Add(new Language() { Name = "Français", Value = "fr", Id = 2 });
            languages.Add(new Language() { Name = "日本語", Value = "ja-JP", Id = 4 });

            return languages;
        }

        public void SetLanguage(int languageId)
        {
            _currentLanguageId = languageId;

            var currentCulture = GetCurrentCultureInfo();
            currentCulture.DateTimeFormat.ShortDatePattern = "MM/dd/yyyy";//andy 20190925 统一workflow xaml中日期格式，避免加载时异常。
            WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.SetCurrentThreadCulture = true;
            WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.Culture = currentCulture;
        }

        public CultureInfo GetCurrentCultureInfo()
        {
            var languages = GetLanguages();
            var value = languages.FirstOrDefault(item => item.Name == "English")?.Value;

            var language = languages.FirstOrDefault(item => item.Id == _currentLanguageId);
            if (language != null)
            {
                value = language.Value;
            }
            var culture = System.Globalization.CultureInfo.CreateSpecificCulture(value); ;

            return culture;
        }

        public int GetCurrentThreadLanguageId()
        {
            var languages = GetLanguages();
            var langueId = languages.FirstOrDefault(item => item.Name == "English").Id;
            var language = languages.Where(item => item.Value == Thread.CurrentThread.CurrentCulture.Name).FirstOrDefault();

            if (language != null)
            {
                langueId = language.Id;
            }

            return langueId;
        }
    }
}
